<?php
    /* visualizza la lista dei giocatori di una rosa*/
   	ini_set('display_errors', true);
	require_once(dirname(__FILE__) . "/../controllers/squadra.php");
	require_once(dirname(__FILE__) . "/../controllers/calciatore.php");
	require_once(dirname(__FILE__) . "/../controllers/lega.php");
	require_once(dirname(__FILE__) . "/../model/db_driver.php");
	session_start();
	if(User::getCurrentUser()==null || User::getCurrentUser()->getRuolo()<User::$ADMIN){
		echo "Errore: devi essere un amministratore per visualizzare questa pagina.<br />";
		die();
	}
	if(isset($_POST['cmd'])){
		if($_POST['cmd']=='changeStatus'){
			$playerId = $_POST['playerId'];
			$teamId = $_POST['teamId'];
			$status = $_POST['status'];
			Squadra::cambiaStatoGiocatore($playerId, $teamId, $status);
			die();
		}elseif($_POST['cmd']=='addPlayer'){
			$playerId = $_POST['playerId'];
			$teamId = $_POST['teamId'];
			$costo = $_POST['costo'];
			Squadra::aggiungiGiocatore($playerId, $teamId, $costo);
			//QUI QUALCOSA ANCORA NON VA
		}
		
	}
	
	
	$teamId=$_POST['teamId'];
	$stagione = "";
	if(isset($_POST['stagione'])){
		$stagione = $_POST['stagione'];
	}else{
		$stagione = Calendario::getStagione();
	}
	$s = Squadra::caricaSquadra($teamId,$stagione);
	
	function getComboStato($playerId, $stato){
		global $teamId;
		$stati = array('A' => 'In rosa', 'F' => 'Fuori rosa','P' => 'In prestito','V' => 'Venduto');
		$options='';
		$sel='';
		foreach($stati as $s => $desc){
			if($stato==$desc){
				$sel='selected=selected';
			}else{
				$sel='';
			}
			$options = $options . "\n<option $sel value='$s'>$desc</option>";
		}
		return "
			<select name='cmbStato_$playerId' onchange='changePlayerState(this,$playerId, $teamId)'>
			" . $options . "
			</select>
		";
	}
	
	function getComboFreePlayers(){
		global $s;
		global $stagione;
		$free_players = $s->getFreePlayers($stagione);
		$options='';
		foreach ($free_players as $p){
			$id=$p->getId();
			$ruolo = $p->getRuolo();
			$nome = $p->getNome();
			$squadra = $p->getSquadra();
			$options = $options . "
				<option style='width:250px;' value='$id'>$ruolo - $squadra - $nome</option>
			";
		} //end foreach
		return "
		<select name='cmbFreePlayer' size='10' multiple='true' style='display:none; height:180px; position:absolute;' id ='cmbFreePlayer' onclick='cmbFreePlayer_click()'>
			$options
		</select>
		";
		
	}
	
		
?>
		<p class='' style='color:green;font-weight:bold;font-size:14px;text-align:center;'>
				Calciatori della squadra <?php echo $s->getNome();?><br />
				stagione: <?php echo $stagione; ?> 
		</p>
		<input type='hidden' name='hTeamId' id='hTeamId' value='<?php echo $s->getId(); ?>' />
		<div style='overflow-y:scroll;width:100%;height:300px;border-top:solid 1px gray;'>
			<table style='width:100%;font-size:10px;' cellpadding='0' cellspacing='0'>
				<tr>
					<td class='roseHeader' style="width:56px;">Ruolo</td>
					<td class='roseHeader' style="width:95px;">Calciatore</td>
					<td class='roseHeader' style="width:85px;">Squadra</td>
					<td class='roseHeader' style="width:65px;">Costo</td>
					<td class='roseHeader' style="width:119px;">Stato</td>
				</tr>
			<!--</table>
		
				 <table style='width:100%;font-size:9px;' cellpadding='0' cellspacing='0'> -->
				<?php 
					for($i=0;$i<$s->getNumGiocatori();$i++){
						$g = $s->getGiocatore($i);
						$id = $g->getId();
						$g = $s->getGiocatore($i);
						$ruolo = $g->getRuolo();
						$nome = $g->getNome();
						$squadra = $g->getSquadra();
						$costo = $g->getCosto();
						$stato = $g->getStato();
						$tId = "roseRow_" . ($i%2);
						echo "
						<tr>
							<td id='$tId' class='rose' style='text-align:center;width:56px'>$ruolo</td>
							<td id='$tId' class='rose' style='width:95px'>$nome</td>
							<td id='$tId' class='rose' style='width:85px'>$squadra</td>
							<td id='$tId' class='rose' style='width:65px'>$costo</td>
							<td id='$tId' class='rose' style='width:119px;'>" . getComboStato($id, $stato) . "</td>
						</tr>
						";
					}
				?>
			</table>
		</div>
		<div style='width:100%;text-align:left;padding-left:5px;border-top:solid 1px gray'>
			<p  style='color:green;font-weight:bold;font-size:14px;text-align:center;'>
					Aggiungi calciatore alla squadra
			</p>
			<table style='width:100%' cellpadding='0' cellspacing='0'>
				<tr>
					<td style='width:70%'>
						<input type='text' id='txtFindPlayer' style='width:250px;' onblur="hideFreePlayerList()" onkeyup='txtFindPlayer_changed(event)' /> <br />
						<?php echo getComboFreePlayers(); ?>
					</td>
					<td style='width:30%;text-align:left;'>
						Costo:&nbsp;<input type='text' name='txtCosto' id = 'txtCosto' value='0' onkeyup='txtCosto_changed()' style='width:40px;text-align:center' />
					</td>
				</tr>
				<tr>
					<td  style='text-align:left;padding-top:10px;'>
						<input type='button' value='Aggiungi' disabled='true' id='btnAddPlayer'
							name='addPlayers' onclick='addPlayerToTeam();' style='color:gray;' >
					</td>
					<td style='text-align:right;padding-right:10px;padding-top:10px;'>
						<input type='button' name='btnCanc' value='Chiudi' onclick='closeDetails("dvTeamPlayers")' style='width:70px;' />
					</td>
				</tr>
				
			</table>
			
		</div>



